package com.team06.controller;

import com.team06.dto.*;
import com.team06.pojo.Airl;
import com.team06.pojo.Blls;
import com.team06.pojo.BllsBelt;
import com.team06.pojo.Dfdl;
import com.team06.service.EmailService;
import com.team06.service.FlightUpdateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/flightUpdate")
public class FlightUpdateController {
    @Autowired
    private FlightUpdateService flightUpdateService;
    @Autowired
    private EmailService emailService;
    // 更新航班飞机信息
    @PutMapping("/updateplanes")
    public void updatePlaneByFlno(@RequestBody FlightUpdateDTO dto) {
        if (dto.getFlno() == null || dto.getFlno().isEmpty()) {
            throw new RuntimeException("必须提供有效的 FLNO");
        }
        flightUpdateService.updatePlaneByFlno(
                dto.getFlno(),
                dto.getCfno(),
                dto.getCftp(),
                dto.getStat()
        );
        System.out.println("通过 FLNO 成功更新航班数据: " + dto.getFlno());
    }
    //更新航班时间信息
    @PutMapping("/updateTimeInfo")
    public void updateFlightAndAirport(@RequestBody FlightTimeInfoDTO dto) {
        if (dto.getFlno() == null || dto.getFlno().isEmpty()) {
            throw new RuntimeException("必须提供有效的 FLNO");
        }
        flightUpdateService.updateFlightAndAirportInfo(
                dto.getFlno(),
                dto.getFide(),
                dto.getStat(),
                dto.getFptt(),
                dto.getFplt()
        );
    }

    //更改航班机场信息
    @PutMapping("/updateAirportInfo")
    public void updateAirportInfo(@RequestBody AirportInfoDTO dto) {
        if (dto.getApotCode() == null || dto.getApotCode().isEmpty()) {
            throw new RuntimeException("必须提供有效的 APOT_CODE");
        }
        flightUpdateService.updateAirportInfo(
                dto.getApotCode(),
                dto.getApotApat(),
                dto.getApotCnnm(),
                dto.getApotEnnm(),
                dto.getApotAiso(),
                dto.getApotApsn()
        );
    }
    // 更新航班航线信息并发送邮件通知
    //请求路径为：  PUT http://localhost:8080/flightUpdate/updateairlines?recipientEmail=1019399421@qq.com
//    @PutMapping("/updateairlines")
//    public void updateairlines(@RequestBody Airl airl, @RequestParam String recipientEmail) {
//        flightUpdateService.updateairlines(airl);
//        System.out.println("航班航线信息更新成功");
//        // 发送通知邮件
//        String subject = "航班航线信息已更新";
//        String body = "航班航线信息已成功更新，请查收最新数据：" + airl.toString();
//        emailService.sendSimpleEmail(recipientEmail, subject, body);
//    }
    //提交申请
    @PostMapping("/submitAirlChange")
    public void submitAirlChange(@RequestBody Airl airl,
                                 @RequestParam String recipientEmail) {
        flightUpdateService.submitAirlChange(airl);

        // 发送通知邮件
        String subject = "航线变更申请已提交";
        String body = "您的航线变更申请已提交，请等待审核：" + airl.toString();
        emailService.sendSimpleEmail(recipientEmail, subject, body);
    }
   //审核通过接口（管理员接口）
   @PostMapping("/approveAirlChange")
   public void approveAirlChange(@RequestParam Integer dfltFlid,
                                 @RequestParam String recipientEmail) {
       flightUpdateService.approveAirlChange(dfltFlid, recipientEmail);

       // 发送通知邮件
       String subject = "航线变更申请已审核通过";
       String body = "您的航线变更申请已审核通过，请查收最新数据。";
       emailService.sendSimpleEmail(recipientEmail, subject, body);
   }
   //审核不通过接口（管理员接口）
   @PostMapping("/rejectAirlChange")
   public void rejectAirlChange(@RequestParam Integer dfltFlid,
                                @RequestParam String recipientEmail) {
       flightUpdateService.rejectAirlChange(dfltFlid);

       // 发送通知邮件
       String subject = "航线变更申请已被拒绝";
       String body = "您的航线变更申请未通过审核，请重新提交或联系管理员。";
       emailService.sendSimpleEmail(recipientEmail, subject, body);
   }
    //行李转盘信息更改
    @PutMapping("/updateBllsBeltByDfltFlid")
    public void updateBllsBeltByDfltFlid(@RequestBody BllsBeltUpdateDTO dto) {
        String dfltFlid = dto.getDfltFlid();
        String btno = dto.getBtno();
        String code = dto.getCode();
        String estr = dto.getEstr();
        String eend = dto.getEend();
        String rstr = dto.getRstr();
        String rend = dto.getRend();
        if (dfltFlid == null || dfltFlid.isEmpty()) {
            throw new RuntimeException("必须提供有效的 DFLT_FLID");
        }
        flightUpdateService.updateBllsBeltByDfltFlidWithParams(dfltFlid, btno, code, estr, eend,rstr, rend);
    }
    //登机口信息更改
    @PutMapping("/updateGtlGateByDfltFlid")
    public void updateGtlGateByDfltFlid(@RequestBody GtlGateUpdateDTO dto) {
        String dfltFlid = dto.getDfltFlid();
        String gtno = dto.getGtno();
        String code = dto.getCode();
        String btsc = dto.getBtsc();
        String estr = dto.getRstr();
        String eend = dto.getRend();
        if (dfltFlid == null || dfltFlid.isEmpty()) {
            throw new RuntimeException("必须提供有效的 DFLT_FLID");
        }
        flightUpdateService.updateGtlGateByDfltFlidWithParams(dfltFlid, gtno, code, btsc, estr, eend);
    }


}